﻿using GameServer;
using GameServer.Networking;
using GameServer.Networking.Packets;
using GameServer.Virtual_Objects.User;
using System;
using System.Text;

namespace GameServer.Networking.Handlers
{
    internal class HANDLE_EQUIPMENT : PacketHandler
    {
        public HANDLE_EQUIPMENT()
        {
        }

        public override void Handle(virtualUser User)
        {
            StringBuilder stringBuilder;
            int i;
            string str;
            object[] objArray;
            bool flag;
            bool flag1;
            bool flag2;
            bool flag3;
            bool flag4;
            try
            {
                User.LoadItems();
                flag = (base.getNextBlock() == "0" ? true : false);
                bool flag5 = flag;
                int num = int.Parse(base.getNextBlock());
                string block = base.getBlock(4);
                int num1 = int.Parse(base.getBlock(5));
                int num2 = int.Parse(base.getBlock(0));
                flag1 = (!block.Contains("-") ? true : num1 != 5);
                if (!flag1)
                {
                    char[] chrArray = new char[] { '-' };
                    string[] strArrays = block.Split(chrArray);
                    User.cCodes6th[num] = string.Join("-", strArrays);
                    flag4 = (!User.hasItem(strArrays[0]) ? true : !User.hasItem(strArrays[1]));
                    if (!flag4)
                    {
                        int[] inventoryID = new int[] { User.getInventoryID(strArrays[0]), User.getInventoryID(strArrays[1]) };
                        string[] strArrays1 = new string[2];
                        if (inventoryID[0] < 10)
                        {
                            strArrays1[0] = string.Concat("I00", inventoryID[0]);
                        }
                        else
                        {
                            strArrays1[0] = string.Concat("I0", inventoryID[0]);
                        }
                        if (inventoryID[1] < 10)
                        {
                            strArrays1[1] = string.Concat("I00", inventoryID[1]);
                        }
                        else
                        {
                            strArrays1[1] = string.Concat("I0", inventoryID[1]);
                        }
                        string str1 = string.Join("-", strArrays1);
                        User.Equipment[num, 5] = str1;
                        User.reloadInventory();
                        stringBuilder = new StringBuilder();
                        for (i = 0; i < 8; i++)
                        {
                            stringBuilder.Append(string.Concat(User.Equipment[num, i], ","));
                        }
                        str = stringBuilder.ToString().Remove(stringBuilder.ToString().Length - 1);
                        User.send(new PACKET_EQUIPMENT(num, str));
                        objArray = new object[] { "UPDATE equipment SET class", num, "='", str, "' WHERE ownerID=", User.UserID.ToString() };
                        DB.runQuery(string.Concat(objArray));
                    }
                    return;
                }
                else if (!flag5)
                {
                    int num3 = int.Parse(base.getBlock(3));
                    flag2 = (num2 != 3 || num1 != -1 ? true : num3 != 5);
                    if (!flag2)
                    {
                        int inventoryID1 = User.getInventoryID(block);
                        string str2 = "^";
                        str2 = (inventoryID1 < 10 ? string.Concat("I00", inventoryID1) : string.Concat("I0", inventoryID1));
                        if (block.Contains("-"))
                        {
                            User.Equipment[num, 5] = "^";
                        }
                        else
                        {
                            User.Equipment[num, 5] = str2;
                        }
                        stringBuilder = new StringBuilder();
                        for (i = 0; i < 8; i++)
                        {
                            stringBuilder.Append(string.Concat(User.Equipment[num, i], ","));
                        }
                        str = stringBuilder.ToString().Remove(stringBuilder.ToString().Length - 1);
                        User.send(new PACKET_EQUIPMENT(num, str));
                        objArray = new object[] { "UPDATE equipment SET class", num, "='", str, "' WHERE ownerID=", User.UserID.ToString() };
                        DB.runQuery(string.Concat(objArray));
                    }
                    else if (User.Equipment[num, num3] == block)
                    {
                        if (User.Equipment[num, num3] != "^")
                        {
                            User.Equipment[num, num3] = "^";
                            stringBuilder = new StringBuilder();
                            for (i = 0; i < 8; i++)
                            {
                                stringBuilder.Append(string.Concat(User.Equipment[num, i], ","));
                            }
                            str = stringBuilder.ToString().Remove(stringBuilder.ToString().Length - 1);
                            User.LoadItems();
                            User.send(new PACKET_EQUIPMENT(num, str));
                            objArray = new object[] { "UPDATE equipment SET class", num, "='", str, "' WHERE ownerID=", User.UserID.ToString() };
                            DB.runQuery(string.Concat(objArray));
                        }
                    }
                }
                else
                {
                    if (User.hasItem(block) || User.PCItem || block == "DA02" && num1 == 0 || block == "DB01" && num1 == 1 || block == "DF01" && num1 == 2 && (num == 0 || num == 1) || block == "DG05" && num1 == 2 && num == 2 || block == "DC02" && num1 == 2 && num == 3 || block == "DJ01" && num1 == 2 && num == 4 || block == "DR01" && num == 0 && num1 == 3 || block == "DQ01" && num == 1 && num1 == 3 || block == "DN01" && (num == 2 || num == 3) && num1 == 3)
                    {
                        flag3 = false;
                    }
                    else
                    {
                        flag3 = (!(block == "DL01") || num != 4 ? true : num1 != 3);
                    }
                    if (!flag3)
                    {
                        if (User.Equipment[num, num1] != block)
                        {
                            for (i = 0; i < 8; i++)
                            {
                                if (User.Equipment[num, i] == block)
                                {
                                    User.Equipment[num, i] = "^";
                                }
                            }
                            User.Equipment[num, num1] = block;
                            stringBuilder = new StringBuilder();
                            for (i = 0; i < 8; i++)
                            {
                                stringBuilder.Append(string.Concat(User.Equipment[num, i], ","));
                            }
                            str = stringBuilder.ToString().Remove(stringBuilder.ToString().Length - 1);
                            User.send(new PACKET_EQUIPMENT(num, str));
                            objArray = new object[] { "UPDATE equipment SET class", num, "='", str, "' WHERE ownerID=", User.UserID.ToString() };
                            DB.runQuery(string.Concat(objArray));
                        }
                    }
                }
            }
            catch (Exception exception)
            {
            }
        }
    }
}